package com.kcht.itsed.mws.aleframework.biz;

import com.kcht.itsed.mws.aleframework.biz.ReaderCommandFactoryGenerator.AbstractReaderCommandFactory;
import com.kcht.itsed.mws.aleframework.intf.IReaderCommandFactory;
import com.kcht.itsed.mws.alemodel.ECSpec;
import com.kcht.itsed.mws.alemodel.TMFixedFieldSpec;
import com.shhic.itsed.mws.deviceintf.RawTagBank;
import com.shhic.itsed.mws.deviceintf.RawTagField;
import com.shhic.itsed.mws.deviceintf.ReaderCommand;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * EC规则的指令生成器
 * @author 张文硕
 *
 */
@Slf4j
public class EcReaderCommandFactory extends AbstractReaderCommandFactory<ECSpec> implements IReaderCommandFactory {
	
	
	public EcReaderCommandFactory(ECSpec spec) {
		super(spec);
	}

	@Override
	public Set<ReaderCommand> generateReaderCommands() {
		//生成EC规则的阅读器命令
		Set<ReaderCommand> rcs = new HashSet<ReaderCommand>();
		
		List<ReaderCommand> rcPrototypes = generateReaderCommandPrototypeOfReaders();
		
		spec.getReportSpecs().getReportSpec().forEach(ecReportSpec->{
			
			if(ecReportSpec.getOutput().getExtension()==null) {
				//没有输出扩展规则，那么就表示只清点
				rcPrototypes.forEach(rt->{
					try {
						rcs.add(rt.clone());
					} catch (CloneNotSupportedException e) {
						log.error("",e);
					}
				});
			} else {
				ecReportSpec.getOutput().getExtension().getFieldList().getField().forEach(ecReportOutputFieldSpec->{
					String fieldName = ecReportOutputFieldSpec.getFieldspec().getFieldname();
					TMFixedFieldSpec fd = tmContainer.getFixedFieldDefination(fieldName);
					rcPrototypes.forEach(rt->{
						try {
							ReaderCommand rc = rt.clone();
							RawTagField rtf = new RawTagField(RawTagBank.fromTMSpecCode(fd.getBank()),fd.getOffset(),fd.getLength());
							rc.getReadTagFieldList().add(rtf);
							rcs.add(rc);
						} catch (Exception e) {
							log.error("",e);
						}
					});
				});
			}
		});
		return rcs;
	}
}
